package co.yixiang.retail.biz.product.mapper;

import co.yixiang.common.mproot.common.mapper.CoreMapper;
import co.yixiang.retail.common.domain.YxStoreProduct;
import co.yixiang.retail.common.domain.YxSystemStore;
import co.yixiang.retail.common.dto.YxStoreProductQueryCriteria;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface YxStoreProductMapper  extends CoreMapper<YxStoreProduct> {

    @Select("<script>select \n" +
            "x.id,\n" +
            "y.store_name,\n" +
            "x.sku,\n" +
            "x.product_id,\n" +
            "z.stock,\n" +
            "x.image,\n" +
            "x.price,\n" +
            "x.ot_price,\n" +
            "z.store_id\n" +
            "FROM yx_store_product y " +
            "left join yx_store_product_attr_value x on y.id = x.product_id " +
            "left join yx_store_product_stock_value z on x.id = z.sku_id  " +
            "where z.store_id = #{criteria.id} and z.stock !=0 " +
            "<if test=\" criteria.storeName !=null and criteria.storeName !='' \">and y.store_name like\"%\"#{criteria.storeName}\"%\" </if>" +
            "</script>")
    List<YxStoreProduct> queryGoods(@Param("criteria") YxStoreProductQueryCriteria criteria );
    @Select("select x.id, " +
            "y.store_name," +
            " x.sku, x.product_id," +
            " z.stock, x.image," +
            " x.price, x.ot_price," +
            "z.product_id,z.store_id FROM " +
            "yx_store_product y left join yx_store_product_attr_value x" +
            " on y.id = x.product_id left join yx_store_product_stock_value z" +
            " on x.id = z.sku_id " +
            "where z.store_id = #{criteria.id} and y.store_name  like concat(\"%\",#{criteria.storeName},\"%\") ")
    List<YxStoreProduct> query(@Param("criteria") YxStoreProductQueryCriteria criteria);

    @Select("select id,name,phone,address,retail_user,user_id from yx_system_store  where id =#{id}")
    YxSystemStore queryRetail(Integer id);

    @Insert("<script>insert into yx_store_order_attr_value (oid,product_id,price,ot_price,amount,sku_id) values <foreach collection=\"products\"  item=\"item\"  separator=\",\" >" +
                                                          "(#{item.oid},#{item.productId},#{item.price},#{item.otPrice},#{item.num},#{item.id})</foreach></script> ")
    void add(@Param("products") List<YxStoreProduct> products);

        @Select("<script>SELECT y.id,s.store_name,y.sku,y.stock,y.product_id,y.price,y.image,y.ot_price FROM yx_store_product s left join \n" +
                "yx_store_product_attr_value y on s.id = y.product_id where  s.is_permit=1  and y.stock != 0<if test=\"criteria.storeName !=null and criteria.storeName !=''\"> and s.store_name like concat(\"%\",#{criteria.storeName},\"%\")</if></script>  ")
    List<YxStoreProduct>  queryByCebters(@Param("criteria") YxStoreProductQueryCriteria criteria);

        @Select("select store_balance from yx_user where uid = #{uid}")
    BigDecimal queryMoney(@Param("uid")Long uid);
//    @Update("update yx_store_product_attr_value a, yx_store_product_stock_value c set a.stock = a.stock - #{amount} where c.storeId = #{merId} and c.skuId = #{id}  and c.skuId = a.id")
    @Update("UPDATE yx_store_product_stock_value c set c.stock = c.stock - ${amount} where c.store_id = #{storeId} and c.sku_id = #{id}")
    void updateRetailStock(@Param("id")Long id,@Param("amount")Integer amount,@Param("storeId") Integer storeId);

    @Update("update yx_store_product_attr_value set stock = stock -#{amount} where id = #{id}")
    void updateCenterStock(@Param("id")Long id, @Param("amount")Integer amount);

    @Insert("<script>insert into yx_store_purchase_log (store_id,pro_store_id,store_name,phone_num,pro_id,pro_name,order_id,pro_num,pro_price,create_time) values <foreach collection=\"products\"  item=\"item\"  separator=\",\">" +
                                                     "(#{item.buyStoreId},#{item.storeId},#{item.name},#{item.phone},#{item.productId},#{item.storeName},#{item.orderId},#{item.num},#{item.price},now())</foreach></script>"
     )
    void addLog(@Param("products") List<YxStoreProduct> products);

    @Insert("<script>insert into yx_store_sales_log (store_id,store_name,buyer_phone,buyer_name,pro_id,pro_name,order_id,pro_num,pro_price,create_time) values <foreach collection=\"products\"  item=\"item\"  separator=\",\">" +
            "(#{item.storeId},#{item.name},#{item.phone},#{item.realName},#{item.productId},#{item.storeName},#{item.orderId},#{item.num},#{item.price},now())</foreach></script>")
    void addSalesLog(@Param("products")List<YxStoreProduct> products);


}
